/**
 * Copyright (c) 2012 Nokia Corporation.
 */

package com.nokia.example.simplepurchase.uicomponents;

import javax.microedition.lcdui.Graphics;

/**
 * The step indicator for non-touch Series 40 devices.
 */
public class StepIndicatorLow extends AbstractStepIndicator {
	// Constants
	private static final int MARGIN = 3;
	private static final int STEP_IMAGE_SIZE = 6;
	private static final int HEIGHT = STEP_IMAGE_SIZE + MARGIN * 2;
	private static final int STEP_INDICATOR_BACKGROUND_COLOR = 0x404040;
	
	/**
	 * Constructor.
	 */
	public StepIndicatorLow(int width, int stepCount) {
		super(width, HEIGHT, stepCount);
	}
	
	/**
	 * From CanvasGraphicsItem.
	 */
	public void paint(Graphics graphics) {
		// Draw background
		graphics.setColor(STEP_INDICATOR_BACKGROUND_COLOR);
		graphics.fillRect(0, 0, _width, _height);
		
		// Draw steps
		final int startX =
				(_width - (STEP_IMAGE_SIZE * _stepCount
						+ MARGIN * (_stepCount - 1))) / 2;
		
		for (int i = 0; i < _stepCount; ++i) {
			drawStep(graphics, startX + i * (STEP_IMAGE_SIZE + MARGIN),
					 MARGIN, (_currentStep == i + 1));
		}
	}
	
	/**
	 * Draws a single step indicator.
	 * @param graphics
	 * @param x
	 * @param y
	 * @param isCurrent
	 */
	private void drawStep(Graphics graphics, int x, int y, boolean isCurrent) {
		// Draw the edges
		graphics.setColor(0xffffff);
		final int length = STEP_IMAGE_SIZE - 1;
		graphics.drawLine(x, y + 1, x, y + STEP_IMAGE_SIZE - 2);
		graphics.drawLine(x + length, y + 1, x + length, y + STEP_IMAGE_SIZE - 2);
		graphics.drawLine(x + 1, y, x + STEP_IMAGE_SIZE - 2, y);
		graphics.drawLine(x + 1, y + length, x + STEP_IMAGE_SIZE - 2, y + length);
		
		if (isCurrent) {
			// Fill
			graphics.fillRect(x + 1, y + 1, STEP_IMAGE_SIZE - 2, STEP_IMAGE_SIZE - 2);
		}
	}
}
